load("//bazel:macros.bzl", "py_binary")
load("//bazel:skia_rules.bzl", "skia_cc_library", "skia_objc_library")

package(
    default_applicable_licenses = ["//:license"],
)

licenses(["notice"])

exports_files([
    "ToolUtils.h",
    "ToolUtils.cpp",
    "SkMetaData.h",
    "SkMetaData.cpp",
    "Registry.h",
    "Resources.h",
    "Resources.cpp",
    "ResourceFactory.h",
])

skia_cc_library(
    name = "registry",
    hdrs = ["Registry.h"],
    visibility = [
        "//bench:__pkg__",
        "//gm:__pkg__",
        "//tests:__subpackages__",
        "//tools/testrunners/benchmark:__pkg__",
        "//tools/viewer:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "hash_and_encode",
    testonly = True,
    srcs = ["HashAndEncode.cpp"],
    hdrs = ["HashAndEncode.h"],
    visibility = [
        "//dm:__pkg__",
        "//gm:__pkg__",
    ],
    deps = [
        "//:core",
        "//:png_encode_codec",
        "//tools/flags:cmd_flags",
    ],
)

py_binary(
    name = "embed_resources",
    srcs = ["embed_resources.py"],
    visibility = ["//:__subpackages__"],
)

skia_cc_library(
    name = "autorelease_pool",
    hdrs = ["AutoreleasePool.h"],
    visibility = [
        "//bench:__pkg__",
        "//dm:__pkg__",
        "//tools/gpu:__pkg__",
        "//tools/testrunners/benchmark:__pkg__",
    ],
    deps = ["//src/base"],
)

skia_objc_library(
    name = "autorelease_pool_objc",
    hdrs = ["AutoreleasePool.h"],
    non_arc_srcs = ["AutoreleasePool.mm"],
    visibility = ["//tools/ganesh:__pkg__"],
    deps = ["//src/base"],
)

skia_cc_library(
    name = "url_data_manager",
    srcs = ["UrlDataManager.cpp"],
    hdrs = ["UrlDataManager.h"],
    visibility = [
        "//dm:__pkg__",
        "//tools/debugger:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "test_font_data_provider",
    testonly = True,
    srcs = ["TestFontDataProvider.cpp"],
    hdrs = ["TestFontDataProvider.h"],
    visibility = ["//dm:__pkg__"],
    deps = [
        "//:core",
        "//modules/jsonreader",
        "//tools/flags:cmd_flags",
    ],
)

skia_cc_library(
    name = "sk_sharing_proc",
    srcs = ["SkSharingProc.cpp"],
    hdrs = ["SkSharingProc.h"],
    defines = ["SK_DISABLE_LEGACY_NONCONST_SERIAL_PROCS"],
    visibility = ["//tools/debugger:__pkg__"],
    deps = [
        "//:core",
        "//:png_decode_codec",
        "//:png_encode_codec",
    ],
)

skia_cc_library(
    name = "mskp_player",
    testonly = True,
    srcs = ["MSKPPlayer.cpp"],
    hdrs = ["MSKPPlayer.h"],
    visibility = ["//tools/viewer:__pkg__"],
    deps = [
        ":sk_sharing_proc",
        "//src/gpu/ganesh:ganesh_TEST_UTIL",
        "//src/utils:multi_picture_document",
    ],
)

skia_cc_library(
    name = "runtime_blend_utils",
    srcs = ["RuntimeBlendUtils.cpp"],
    hdrs = ["RuntimeBlendUtils.h"],
    visibility = [
        "//dm:__pkg__",
        "//tests:__pkg__",
        "//tools/viewer:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "stats",
    srcs = ["ProcStats.cpp"],
    hdrs = [
        "ProcStats.h",
        "Stats.h",
    ],
    visibility = [
        "//bench:__pkg__",
        "//dm:__pkg__",
        "//tools/testrunners/benchmark:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "tool_utils",
    testonly = True,
    srcs = [
        "SkMetaData.cpp",
        "ToolUtils.cpp",
    ],
    hdrs = [
        "SkMetaData.h",
        "ToolUtils.h",
    ],
    visibility = [
        "//bench:__subpackages__",
        "//gm:__subpackages__",
        "//tools:__subpackages__",
    ],
    deps = [
        "//:core",
        "//src/base",
        "//src/core:core_priv",
    ],
)

skia_cc_library(
    name = "codec_utils",
    testonly = True,
    srcs = [
        "DecodeUtils.cpp",
        "EncodeUtils.cpp",
    ],
    hdrs = [
        "CodecUtils.h",
        "DecodeUtils.h",
        "EncodeUtils.h",
    ],
    visibility = [
        "//dm:__pkg__",
        "//gm:__subpackages__",
        "//tools/viewer:__pkg__",
    ],
    deps = [
        "//:core",
        "//:png_decode_codec",
        "//:png_encode_codec",
        "//src/base",
        "//src/core:core_priv",
        "//tools:resources",
    ],
)

skia_cc_library(
    name = "resources",
    testonly = True,
    srcs = [
        "Resources.cpp",
    ],
    hdrs = [
        "Resources.h",
    ],
    visibility = [
        "//bench:__subpackages__",
        "//tools:__subpackages__",
    ],
    deps = [
        "//:core",
        "//src/base",
        "//src/core:core_priv",
        "//tools/flags:cmd_flags",
    ],
)

skia_cc_library(
    name = "gpu_tool_utils",
    testonly = True,
    hdrs = ["GpuToolUtils.h"],
    visibility = [
        "//dm:__pkg__",
        "//tools:__subpackages__",
    ],
    deps = [
        "//:core",
    ],
)

# Regenerate workarounds with `bazel run //tools:generate_workarounds`
py_binary(
    name = "generate_workarounds",
    srcs = [":generate_workarounds.py"],
    args = [
        "--output-file",
        "include/gpu/ganesh/GrDriverBugWorkaroundsAutogen.h",
        "src/gpu/gpu_workaround_list.txt",
    ],
    data = [
        ":build_workaround_header",
    ],
    tags = ["no-remote"],
)

py_binary(
    name = "build_workaround_header",
    srcs = ["build_workaround_header.py"],
)

_GENERATE_WORKAROUNDS = """
import os
import subprocess
import sys

# https://bazel.build/docs/user-manual#running-executables
# Note: Bazel eats single quotes, so we must use double quotes.
os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"])

# execpath returns the path to the given label relative to the Skia root.
# This will be something like:
#   bazel-out/k8-opt-exec-81C6BA4F/bin/tools/build_workaround_header
# https://bazel.build/reference/be/make-variables#predefined_label_variables
generate_script = os.path.abspath("$(execpath :build_workaround_header)")

result = subprocess.run(
    [generate_script] + sys.argv[1:], capture_output=True, encoding="utf-8")
if result.returncode != 0:
    print(result.stdout)
    print(result.stderr)
    sys.exit(result.returncode)
"""

genrule(
    name = "create_generate_workarounds_script",
    outs = ["generate_workarounds.py"],
    cmd = "echo '%s' > $@" % _GENERATE_WORKAROUNDS,
    tools = [":build_workaround_header"],
)

skia_cc_library(
    name = "full_build",
    testonly = True,
    srcs = [
        "//include/codec:headers_to_compile",
        "//include/core:headers_to_compile",
        "//include/docs:headers_to_compile",
        "//include/effects:headers_to_compile",
        "//include/encode:headers_to_compile",
        "//include/gpu:headers_to_compile",
        "//include/gpu/ganesh:headers_to_compile",
        "//include/gpu/ganesh/gl:headers_to_compile",
        "//include/gpu/ganesh/mock:headers_to_compile",
        "//include/gpu/ganesh/vk:headers_to_compile",
        "//include/gpu/graphite:headers_to_compile",
        "//include/gpu/graphite/precompile:headers_to_compile",
        "//include/gpu/graphite/vk:headers_to_compile",
        "//include/gpu/graphite/vk/precompile:headers_to_compile",
        "//include/gpu/vk:headers_to_compile",
        "//include/pathops:headers_to_compile",
        "//include/ports:headers_to_compile",
        "//include/private:headers_to_compile",
        "//include/private/base:headers_to_compile",
        "//include/sksl:headers_to_compile",
        "//include/svg:headers_to_compile",
        "//include/utils:headers_to_compile",
        "//src/base:headers_to_compile",
        "//src/codec:headers_to_compile",
        "//src/core:headers_to_compile",
    ],
    copts = [
        # We don't want errors like "unused symbol"
        "-Wno-error",
    ],
    # This is not intended to be used for tests or anything, just for a smoke test build
    visibility = ["//visibility:private"],
    deps = [
        "//:bmp_decode_codec",
        "//:core",
        "//:fontmgr_data_freetype",
        "//:fontmgr_directory_freetype",
        "//:fontmgr_empty_fontations",
        "//:fontmgr_empty_freetype",
        "//:gif_decode_codec",
        "//:ico_decode_codec",
        "//:jpeg_decode_codec",
        "//:jpeg_encode_codec",
        "//:jpegxl_decode_codec",
        "//:pathops",
        "//:pdf_writer",
        "//:png_decode_codec",
        "//:png_encode_libpng",
        "//:png_encode_rust",
        "//:skparagraph_harfbuzz_skunicode",
        "//:skresources",
        "//:skshaper_core",
        "//:skshaper_harfbuzz",
        "//:skshaper_unicode",
        "//:skunicode_core",
        "//:skunicode_icu",
        "//:svg_renderer",
        "//:svg_writer",
        "//:wbmp_decode_codec",
        "//:webp_decode_codec",
        "//:webp_encode_codec",
        "//modules/jsonreader",
        "//src/codec:android_codec",
        "//src/gpu/ganesh/gl:ganesh_gl_TEST_UTIL",
        "//src/gpu/ganesh/vk:ganesh_vulkan_TEST_UTIL",
        "//src/gpu/graphite:graphite_TEST_UTIL",
        "//src/gpu/graphite/precompile:precompile_TEST_UTIL",
        "//src/gpu/graphite/vk:graphite_native_vulkan_TEST_UTIL",
        "//tools/sksltrace:sksltraceutils",
    ],
)
